package dbinit;

import java.util.HashSet;
import java.util.List;
import java.util.Iterator ;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import core.price.model.Price;
import core.product.model.Product;
import core.product.model.ProductSearchKey;
import core.product.model.ProductType;
import core.util.HibernateCoreUtil;

public class InitProduct 
{
	public static Product getProductByUuid(Long uuid)
	{
		Product p = null ;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try 
		{
				transaction = session.beginTransaction();
				p = (Product) session.get(Product.class, uuid);
				transaction.commit();
		}
		catch (HibernateException e) 
		{
			// TODO: handle exception
			transaction.rollback();
			e.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		return p ;
	}
	public static Long getProductIdBySku(String sku)
	{
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Long uuid = null ;
		Product p = null ;
		try
		{
			transaction = session.beginTransaction();
			List <Product> ps = session.createQuery("from Product p where p.sku='"+sku+"'").list();
			 if(ps.size() != 0)
			 {
				 Iterator <Product> psItr = ps.iterator(); 
				 while(psItr.hasNext())
				 {
					 p = psItr.next() ;
					 break ;
				 }
				 uuid = p.getUuid();
			 }
			 transaction.commit();
		}
		catch(HibernateException e)
		{
			e.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		return uuid ;
	}
	public static void updateProductSearchKey(Product p , ProductSearchKey psk)
	{
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		try
		{
			transaction = session.beginTransaction();
			p.getProductSearchKeys().add(psk);
			session.merge(p);
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally 
		{
			session.close();
		}
	}
	
	public static void updateProductPrice(Product p , Iterator<Long> priceIditr)
	{
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		try
		{
			transaction = session.beginTransaction();
			Set<Price> prices = new HashSet<Price>();
			if(priceIditr != null)
			{
				while(priceIditr.hasNext())
				{
					prices.add(InitPrice.getPriceByUuid(priceIditr.next()));
				}
			}
			p.setPrice(prices);
			session.saveOrUpdate(p);
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally 
		{
			session.close();
		}
	}

	public static Long doinit(String sku, String name, String imagePath, String thumbnail,Long productType,Iterator<Long> priceIditr, boolean avalable, boolean enabled)
	{
		Long uuid = getProductIdBySku(sku) ;
		if(uuid == null)
		{
			Set<ProductType>ProductT = new HashSet<ProductType>();
			Transaction transaction = null;
			Session session = HibernateCoreUtil.getSessionFactory().openSession();
			Product p = new Product();
			p.setSku(sku);
			p.setName(name);
			p.setImagePath(imagePath);
			p.setThumbnail(thumbnail);
			ProductT.add(InitProductType.getProductTypeByUuid(productType));
			p.setProductType(ProductT);
			Set<Price> prices = new HashSet<Price>();
			if(priceIditr != null)
			{
				while(priceIditr.hasNext())
				{
					prices.add(InitPrice.getPriceByUuid(priceIditr.next()));
				}
			}
			p.setPrice(prices);
			p.setAvalable(avalable);
			p.setEnabled(enabled);
			try
			{
				transaction = session.beginTransaction();
				uuid = (Long)session.save(p);
				transaction.commit();
			}
			catch(HibernateException he)
			{
				he.printStackTrace();
			}
			finally 
			{
				session.close();
			}
		}
		
		return uuid ;
	}
}
